package com.wfh.frp.handler;

import com.wfh.frp.model.ProxyMsg;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @author fenghuanwang
 */
public class ProxyToServerHandler extends SimpleChannelInboundHandler<ProxyMsg> {
    private final Channel visitorChannel;

    public ProxyToServerHandler(Channel visitorChannel) {
        this.visitorChannel = visitorChannel;
    }

    /**
     * 收到消息
     * @param ctx
     * @param msg
     */
    @Override
    protected void messageReceived(ChannelHandlerContext ctx, ProxyMsg msg) {
        if (msg.getType() == ProxyMsg.TRANSFER) {
            // 来自 Server 的数据 → 转给 Visitor
            visitorChannel.writeAndFlush(msg);
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) {
        visitorChannel.close();
    }
}